package com.baikeyang.app.controller.admin.activity;


import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.model.ActivityPhotoAlbum;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.activity.IActivityPhotoAlbumService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 活动照片相册 前端控制器
 * </p>
 *
 * @author BaiKeyang
 * @since 2019-05-21
 */
@RestController(value = "activityPhotoAlbumController")
@RequestMapping("/admin/activity/album")
public class ActivityPhotoAlbumController {

    @Autowired
    private IActivityPhotoAlbumService albumService;

    @RequestMapping(value = "index")
    public ModelAndView albumIndex () {
        return new ModelAndView("/admin/activity/album/activity-album-index");
    }

    @RequestMapping(value = "list")
    public PageResult list (String page, String recPerPage, String search, String sortBy, String order) {
        PageResult pagers = new PageResult();
        try{
            Integer size = 10;
            Integer current = 1;
            if(StringUtils.isNotEmpty(recPerPage)){
                try {
                    size = Integer.parseInt(recPerPage);
                } catch (Exception e) {}
            }
            if(StringUtils.isNotEmpty(page)) {
                try {
                    current = Integer.parseInt(page);
                    if(current < 1){current = 1;}
                } catch (Exception e) {}
            }
            Page<ActivityPhotoAlbum> pageParam = new Page<>(current, size);
            IPage<ActivityPhotoAlbum> pageResult = albumService.loadActivityPhotoAlbumPage(pageParam);
            pagers.setData(pageResult.getRecords());
            pagers.setPager(current, pageResult.getTotal());
            pagers.getPager().setRecPerPage(Integer.parseInt(recPerPage));
            pagers.setResult("success");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return pagers;
    }

    @RequestMapping(value = "add")
    public ModelAndView add () {
        return new ModelAndView("/admin/activity/album/activity-album-add");
    }

    @RequestMapping(value = "edit/{id}")
    public ModelAndView edit (@PathVariable int id) {
        return new ModelAndView("/admin/activity/album/activity-album-edit")
                .addObject("activityType", albumService.getById(id));
    }

    @RequestMapping(value = "save")
    public ResponseResult save (ActivityPhotoAlbum album) {
        return albumService.saveActivityPhotoAlbum(album);
    }

    @RequestMapping(value = "update")
    public ResponseResult update (ActivityPhotoAlbum album) {
        return albumService.updateActivityPhotoAlbum(album);
    }

    @RequestMapping(value = "del/{id}")
    public ResponseResult del (@PathVariable int id) {
        return albumService.deleteActivityPhotoAlbum(id);
    }

    @RequestMapping(value = "detail/{id}")
    public ModelAndView detail (@PathVariable int id) {
        return new ModelAndView("/admin/activity/album/activity-album-detail")
            .addObject("album", albumService.getById(id))
            .addObject("files", albumService.loadActivityFileLibByAlbum(id));
    }

    @RequestMapping(value = "upload/{id}")
    public ModelAndView upload (@PathVariable int id) {
        return new ModelAndView("/admin/activity/album/activity-album-upload")
            .addObject("album", albumService.getById(id));
    }

}

